package com.bsoft.gol.hcs.specialist.service.impl;

import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevReference;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import com.bsoft.gol.hcs.enumcase.DiagnosisAndTreatmentDataTypeEnum;
import hcn.bean.UserAllVo;
import org.springframework.beans.factory.annotation.Autowired;
import pcn.service.web.BaseDeviceService;
import pcn.vo.web.Page;
import service.rpc.IServiceOpenRpcService;
import com.bsoft.gol.hcs.specialist.bean.request.VrSynchroSettingsReq;
import com.bsoft.gol.hcs.specialist.service.IVrSynchroSettingsService;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.visitrecord.dao.VrSynchroSettingsDAO;
import com.bsoft.gol.hcs.visitrecord.entity.VrSynchroSettings;

import java.util.*;

/**
 * @Author: lanyu
 * @Description: 诊疗数据同步service实现类
 * @Date: create in 2022/5/31 18:58
 */
@SsdevService("vrSynchroSettings")
public class VrSynchroSettingsServiceImpl implements IVrSynchroSettingsService {

    @Autowired
    private VrSynchroSettingsDAO vrSynchroSettingsDAO;

    @SsdevReference("hcn.baseDeviceService")
    private BaseDeviceService baseDeviceService;

    @Autowired
    IServiceOpenRpcService serviceOpenRpcService;

    /**
     * 诊疗数据同步数据获取
     *
     * @return
     */
    @Override
    @RpcService
    public List<VrSynchroSettings> queryVrSynchroSettingsList(VrSynchroSettingsReq request) throws ControllerException {
        //1.查询该机构是否开通了随访权限
        if (CommonUtils.isEmpty(request.getOrganizationId()) || CommonUtils.isEmpty(request.getOrgId())) {
            throw new ControllerException("机构id不能为空");
        }
        String organizationId = request.getOrganizationId();
        Page page = new Page();
        page.setPageNo(1);
        page.setPageSize(10);
        List<String> serviceCodeList = Arrays.asList("0131", "0145", "0146");
        page.setServiceCodeList(serviceCodeList);
        page.setOrgId(organizationId);
        Map<String, Object> orgServiceListWeb = null;
        List<VrSynchroSettings> vrSynchroSettings = new ArrayList<>();
        try {
            orgServiceListWeb = serviceOpenRpcService.getOrgServiceListWeb(page);
            if (CommonUtils.isNotEmpty(orgServiceListWeb.get("list"))) {
                //开通了随访权限的机构，若默认查询为空，则进行初始化
                vrSynchroSettings = vrSynchroSettingsDAO.queryVrSynchroSettingsListByOrgId(request.getOrgId());
                if (CommonUtils.isEmpty(vrSynchroSettings)) {
                    //遍历进行初始化
                    for (DiagnosisAndTreatmentDataTypeEnum value : DiagnosisAndTreatmentDataTypeEnum.values()) {
                        if (value.getType() <= 9) {
                            VrSynchroSettings vrSynchroSetting = new VrSynchroSettings();
                            vrSynchroSetting.setOrgId(request.getOrgId());
                            vrSynchroSetting.setSettingType(value.getSettingType());
                            vrSynchroSetting.setDateType(value.getText());
                            vrSynchroSetting.setBeginTime(new Date());
                            vrSynchroSetting.setStorageMonth(0);
                            VrSynchroSettings save = vrSynchroSettingsDAO.save(vrSynchroSetting);
                            vrSynchroSettings.add(save);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vrSynchroSettings;
    }

    /**
     * 诊疗数据同步设置修改
     *
     * @param vrSynchroSettingsList
     * @throws ControllerException
     */
    @Override
    @RpcService
    public void updateVrSynchroSettings(List<VrSynchroSettings> vrSynchroSettingsList) throws ControllerException {
        if (CommonUtils.isEmpty(vrSynchroSettingsList)) {
            throw new ControllerException("诊疗数据信息不能为空");
        }
        for (VrSynchroSettings vrSynchroSetting : vrSynchroSettingsList) {
            //添加修改人信息
            UserAllVo allByOnline = baseDeviceService.getAllByOnline();
            vrSynchroSetting.setLastModifyUserId(allByOnline.getUserId());
            vrSynchroSetting.setLastModifyUserName(allByOnline.getUserName());
            vrSynchroSetting.setLastModifyTime(new Date());
            vrSynchroSettingsDAO.update(vrSynchroSetting);
        }
    }
}
